



#import "TabBarController.h"
//图标
#define HomeImage           @"tab_home_disable"
#define HomeSelectImage     @"tab_home_highlight"
#define FindImage           @"tab_shopping_disable"
#define FindSelectImage     @"tab_shopping_highlight"
#define MineImage           @"tab_mine_disable"
#define MineSelectImage     @"tab_mine_highlight"
//文本
#define TabBar_TextColor_normal         [UIColor blackColor];
#define TabBar_TextColor_highlighted    [UIColor redColor];

@interface TabBarController ()<UITabBarControllerDelegate>//添加协议

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //调用创建标签栏
    [self SetTabBar];
    //调用设置标签栏
    [self TabBarUp];
}
#pragma makr -- 设置标签栏
-(void)TabBarUp
{
    //标签栏背景颜色
        self.tabBar.barTintColor = COLOR_RGB(0xffffff, 1);
    //默认界面
    self.selectedIndex = 0;
}
#pragma mark -- 创建标签栏
-(void)SetTabBar
{

    //首页
    
    HomeViewController *home = [[HomeViewController alloc]init];
    [self addChildVC:home title:@"首页" image:HomeImage selectImage:HomeSelectImage];


    //购物车
    
    ShoppingCartViewController *shoppingcart = [[ShoppingCartViewController alloc]init];
    [self addChildVC:shoppingcart title:@"购物车" image:FindImage selectImage:FindSelectImage];
    
    //个人
    
    MineViewController *user = [[MineViewController alloc]init];
    [self addChildVC:user title:@"我的" image:MineImage selectImage:MineSelectImage];

    
    //    home.tabBarItem.badgeValue = @"";//加
    //    home.tabBarItem.badgeValue = nil;//减
    
}
#pragma mark -- TabBarDelegate
//点击tabbar的不同模块去判断是否可以使用(是否登录了)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //    static NSInteger lastindex = 0;
    //    NSInteger index = self.selectedIndex;
//    switch (self.selectedIndex) {
//        case 0:
//            NSLog(@"点击了0");
//
//            break;
//        case 1:
//            NSLog(@"点击了1");
//
//            break;
//        case 2:
//            NSLog(@"点击了2");
//
//            break;
//
//        default:
//            break;
//    }
    if (self.selectedIndex == 2)
    {
        NSLog(@"点击了2");
    }
}

#pragma mark -- 创建标签栏方法
/**
 *  实例方法来创建标签栏
 *
 *  @param childVC     传入要添加的界面控制器
 *  @param title       界面的名称字符串
 *  @param image       按钮图片
 *  @param selectImage 选中状态按钮图片
 */
-(void)addChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage;
{
    childVC.title = title;
    //正常按钮图片
    childVC.tabBarItem.image = [UIImage imageNamed:image];
    //位置设置
//    UIEdgeInsets insets = UIEdgeInsetsMake(6, 0, -6, 0);
//    childVC.tabBarItem.imageInsets = insets;
    //选中的按钮颜色,修改渲染
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //正常的字体颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = TabBar_TextColor_normal;
    [childVC.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
    //选中的字体颜色
    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
    selectAttrs[NSForegroundColorAttributeName] =  TabBar_TextColor_highlighted;
    [childVC.tabBarItem setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
    
    //封装导航栏
    NavigationController *Nav = [[NavigationController alloc]initWithRootViewController:childVC];
    //将导航栏添加到标签栏上
    [self addChildViewController:Nav];
    
}


@end

